package org.clockworkmages.games.anno1186;

import java.util.List;

import org.clockworkmages.games.anno1186.gui.FontStyleConstants;
import org.clockworkmages.games.anno1186.model.GameMode;
import org.clockworkmages.games.anno1186.model.character.PlayerCharacter;
import org.clockworkmages.games.anno1186.model.effect.Effect;
import org.clockworkmages.games.anno1186.model.option.GenericOption;
import org.clockworkmages.games.anno1186.model.option.Option;
import org.clockworkmages.games.anno1186.model.situation.Situation;
import org.clockworkmages.games.anno1186.scripting.ScriptingService;
import org.clockworkmages.games.anno1186.text.TextUtil;

public class SituationService {

	@Injected
	private GameStateService gameStateService;

	@Injected
	private GameGuiService gameGuiService;

	@Injected
	private ScriptingService scriptingService;

	@Injected
	private GameCharacterService gameCharacterService;

	/**
	 * The situation that was played previously. This information is stored in
	 * order to avoid clearing the text if we're only switching between the
	 * dialog options of a single situation.
	 */
	private Situation previouslyPlayedSituation = null;

	/**
	 * 
	 */
	/**
	 * 
	 */
	public void playCurrentSituation() {
		GameState gameState = gameStateService.getGameState();
		Situation currentSituation = gameState.getCurrentSituation();
		if (currentSituation == null) {
			// ok, the Situation has finished, we will now go back to the
			// GameMode.LOCAL_MAP
			gameState.setGameMode(GameMode.LOCAL_MAP);
			gameGuiService.getTextPane().clearText();
			return;
		}

		currentSituation.update();

		if (currentSituation != previouslyPlayedSituation) {
			if (currentSituation.isClearScreen()) {
				gameGuiService.getTextPane().clearText();
			}
			boolean currentSituationWasRecentlyAdded = gameStateService
					.getGameState().currentSituationWasRecentlyAdded();
			if (currentSituationWasRecentlyAdded
					&& currentSituation.getEffects() != null) {
				for (Effect effect : currentSituation.getEffects()) {
					if (effect.getCondition() == null
							|| scriptingService.conditionIsMet(effect
									.getCondition())) {
						scriptingService.execute(effect.getEffect());
					}
				}
			}
		}

		List<Option> options = currentSituation.getOptions();

		// CardLayout optionCardLayout = (CardLayout) gameGuiService
		// .getOptionCardPanel().getLayout();

		// Text for the "You see [...]." portion of the generic Situation's
		// description.
		StringBuffer seeTextBuffer = new StringBuffer();

		if (currentSituation != previouslyPlayedSituation) {
			String title = currentSituation.getTitle();
			if (title != null) {
				gameGuiService.getTextPane().appendText(
						"{f:b}" + title + "{f}\n\n");
			}
			gameGuiService.getTextPane().appendText(
					TextUtil.parse(currentSituation.getText()) + "\n");

			if (seeTextBuffer.length() > 0) {
				String seeText = seeTextBuffer.toString();
				seeText = "\nYou see {f:b}"
						+ seeText.substring(0, seeText.length() - 2)
						+ "{f}.+\n\n";
				gameGuiService.getTextPane().appendText(seeText);
			}

			previouslyPlayedSituation = currentSituation;
		}
		gameGuiService.getTextPane().flushInfoBuffer();

		if (gameState.getCharacter().isStatsAreStale()) {
			gameCharacterService.refreshStats(gameState.getCharacter());

		}

		if (options.size() == 1 && options.get(0).isTextfield()) {
			// this situation requires a text input (e.g. naming your character)
			gameGuiService.getOptionTextInputKeyListener().setOption(
					options.get(0));
			// optionCardLayout.show(gameGuiService.getOptionCardPanel(),
			// GameUiConstants.OPTION_PANEL_TEXT_CARD);
			gameGuiService.getTextPane().appendText(" ",
					FontStyleConstants.TEXTINPUT);
		} else {
			// this situation requires the user to select an available option
			// for a list
			gameGuiService.getOptionList().removeAll();

			List<Option> availableOptions = OptionUtil
					.getAvailableOptions(options);

			for (Option option : availableOptions) {
				if (option instanceof GenericOption) {
					String seeText = ((GenericOption) option).getSee();
					if (seeText != null) {
						seeTextBuffer.append(seeText).append(", ");
					}
				}
			}
			gameGuiService.getOptionList().setListData(
					(Option[]) availableOptions.toArray(new Option[] {}));
			gameGuiService.getTextPane().appendText(" ",
					FontStyleConstants.OPTIONLIST);
			// optionCardLayout.show(gameGuiService.getOptionCardPanel(),
			// GameUiConstants.OPTION_PANEL_LIST_CARD);
		}

		gameGuiService.getBottomLeft().repaint();

		PlayerCharacter me = gameState.getCharacter();
		double maxHealth = me.getMaxHealth();
		double healthState = maxHealth == 0 ? 0 : me.getHealth()
				/ me.getMaxHealth();
		gameGuiService
				.getExplorationPanel()
				.getLeftTotemPanel()
				.getTotemImage()
				.setTargetState(healthState > 0 ? healthState : 0,
						2 * TimeConstants.SECOND);

	}
}
